﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using Domain.NetworkService;
using Domain.NetworkRemoteDevice;

namespace Domain.NetworkService
{
    [Serializable]
    class Serializer
    {
        public List<NetworkServiceState> NetworkServiceStates { get; private set; }
        public bool saveNetworkServiceStates(List<NetworkServiceState> networkServiceStates, string fileToSave)
        {
            fileToSave = String.Format(@"{0}.dat", fileToSave);
            BinaryFormatter binFormat = new BinaryFormatter();
            try
            {
                using (Stream fStream = new FileStream(fileToSave, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    binFormat.Serialize(fStream, networkServiceStates);
                }
            }
            // обрабатывается выше
            catch (Exception)
            {
                throw;
            }
            return true;
        }

        public bool loadNetworkServiceStates(string fileToLoad)
        {
            BinaryFormatter binFormat = new BinaryFormatter();
            try
            {
                using (Stream fStream = File.OpenRead(fileToLoad))
                {
                    NetworkServiceStates =
                         (List<NetworkServiceState>)binFormat.Deserialize(fStream);
                }
            }
            // обрабатывается выше
            catch (Exception)
            {
                throw;
            }
            return true;
        }
    }
}
